const moment = require('moment');

let TEST = false
//TEST = true

module.exports = function (app, opts) {
    const notPlanPatrolPlan = app.fs.scheduleInit(
        {
            interval: '0 50 23 * * *',
            immediate: TEST,
            proRun: !TEST,
        },
        async () => {
            try {
                const { models } = app.fs.dc;
                let findOption = {
                    where: {
                        status: { $in: [0, 1] },//待完成的
                        endTime: { $lt: moment().add(1, 'day').format('YYYY-MM-DD 00:00:00') }//今天24点以前的
                    },
                    // include: [
                    //     {
                    //         model: models.UnplannedPatrolTemplate,
                    //         as: 'ttemplates',
                    //         include: [{
                    //             model: models.UnplannedCheckItems,
                    //         }]
                    //     }]
                }
                let plans = await models.UnplannedPatrolPlan.findAll(findOption);
                let pIds = plans.map(p => p.id);



                // let records = await models.UnplannedPatrolRecord.findAll({
                //     where: { planId: { $in: pIds } },
                //     include: [{
                //         model: models.UnplannedRecordTemplates,
                //         include: [{
                //             model: models.UnplannedRecordCheckItems,
                //         }]
                //     }
                //     ],
                // });

                for (let i = 0; i < plans.length; i++) {
                    // let pp = plans[i];
                    // let total = 0, number = 0;//计算当前完成度

                    // pp.ttemplates.forEach(tt => {
                    //     tt.unplannedCheckItems.forEach(cc => {
                    //         total++;
                    //         let rcd = records.find(r => r.planId == pp.id);
                    //         if (rcd && rcd.unplannedRecordTemplates) {
                    //             let rtt = rcd.unplannedRecordTemplates.find(ut => ut.templateId === tt.id);
                    //             if (rtt && rtt.unplannedRecordCheckItems) {
                    //                 let opr = rtt.unplannedRecordCheckItems.find(uc => uc.checkItemId === cc.id);
                    //                 if (opr && opr.result) {
                    //                     number++;
                    //                 }
                    //             }
                    //         }
                    //     })
                    // })

                    // let rate = total ? `${(100 * number / total).toFixed(2)}%` : null;//完成度
                    await models.UnplannedPatrolRecord.update({ completeTime: plans[i].endTime }, {
                        where: { planId: plans[i].id }
                    });

                    // await models.UnplannedPatrolPlan.update({ status: 2, expireConfig: pp.ttemplates, expireRate: rate }, {
                    //     where: { id: pp.id }
                    // })
                }
                await models.UnplannedPatrolPlan.update({ status: 2 }, {
                    where: { id: { $in: pIds } }
                })
            } catch (error) {
                app.fs.logger.error(`sechedule: notPlanPatrolPlan, error: ${error}`);
            }
        }
    );

    return {
        notPlanPatrolPlan
    }
}